﻿using Unity.Entities;

namespace Jinndev.Tile2D {

    public static class SequenceFrameUtil {

        /// <summary>
        /// 切换"开/关"序列动画，通过正反播放当前序列帧实现
        /// </summary>
        public static void ToggleSequence(ref SequenceFrame sequenceFrame, in DynamicBuffer<SequenceBuffer> sequenceBuffer, bool isOn) {
            if (sequenceBuffer.IsCreated && sequenceFrame.sequenceIndex < sequenceBuffer.Length) {
                // 从当前帧向相反播放，而不是直接从头播放
                Sequence sequence = sequenceBuffer[sequenceFrame.sequenceIndex].sequence;
                sequenceFrame.frame = sequence.frames - 1 - sequenceFrame.frame;
            }
            else {
                // 从头播放
                sequenceFrame.frame = 0;
            }

            sequenceFrame.reverse = !isOn;
            sequenceFrame.play = true;
            sequenceFrame.dirty = true;
        }

    }

}
